<?php
require_once('../../config.php');
Doctrine::loadModels('../../models');

if ($_REQUEST["agent_id"]) 
{
	$agent_id = strip_tags($_REQUEST["agent_id"]);
	
	$query = new Doctrine_Query();

	/*
	$query->from('AgentMessage')
    			->where('agent_id ='.$agent_id);*/
    
	$query->select('a.message_id, a.parent_message_id, a.agent_id, 
    					a.message_is_public, a.message_is_comment, a.message, a.created, a.modified,
    					s.item_id subject_id, s.item_name subject, r.item_id rating_id ,r.item_name rating')
    			->from('AgentMessage a')
    			->innerjoin('a.Subject s')
    			->innerjoin('a.Rating r')
    			->where('a.agent_id ='.$agent_id);
    			
    $items = $query->execute();  
    
    file_put_contents  ( "getMessage.txt", $query);
}
else if ($_REQUEST["subject_id"]) 
{	
	$subject_id = strip_tags($_REQUEST["subject_id"]);
	
	$query = new Doctrine_Query();
    $query->select('a.message_id, a.parent_message_id, a.agent_id, 
    					a.message_is_public, a.message_is_comment, a.message, a.created, a.modified,
    					s.item_id subject_id, s.item_name subject, r.item_id rating_id ,r.item_name rating')
    			->from('AgentMessage a')
    			->leftjoin('a.Subject s')
    			->leftjoin('a.Rating r')
    			->where('a.subject_id ='.$subject_id);
    $items = $query->execute();
}

$dom = new DomDocument('1.0');
$dom->formatOutput = true;

$root = $dom->createElement( "results" );
$dom->appendChild( $root );
	foreach ($items as $item){
		$dn = $dom->createElement( "record" );
		$dn->setAttribute( 'message_id', $item['message_id'] );
		$dn->setAttribute( 'parent_message_id', $item['parent_message_id'] );
		$dn->setAttribute( 'agent_id', $item['agent_id'] );
		$dn->setAttribute( 'subject_id', $item['subject_id'] );
		$dn->setAttribute( 'subject', $item['subject'] );
		$dn->setAttribute( 'rating_id', $item['rating_id'] );
		$dn->setAttribute( 'rating', $item['rating'] );
		$dn->setAttribute( 'message_is_public', $item['message_is_public'] );
		$dn->setAttribute( 'message_is_comment', $item['message_is_comment'] );
		$dn->setAttribute( 'message', utf8_encode($item['message']));
		$dn->setAttribute( 'created', utf8_encode($item['created']));
		$dn->setAttribute( 'modified', utf8_encode($item['modified']));
		$root->appendChild( $dn );
}		

header( "Content-type: text/xml; charset=UTF-8" );
echo $dom->saveXML();


?>
	
